package com.vancouver.knows.business.tracker;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.pangu.bean.PageableResult;
import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.business.BUtils;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.DiscountCategoryInfo;
import com.vancouver.knows.business.bean.DiscountInfo;
import com.vancouver.knows.business.bean.EventBannerInfo;
import com.vancouver.knows.business.bean.HomePageInfo;
import com.vancouver.knows.business.service.impl.FetchCityDiscountsHandler;
import com.vancouver.knows.business.task.mark.FetchCityDiscountsTaskMark;

/**
 * 获取城惠首页列表
 * 
 * @author chenxunlin01
 * 
 */
public class FetchCityDiscountsTracker extends ABInvokeTracker {

	public FetchCityDiscountsTracker(BusinessModule module, IResultReceiver resultReceiver) {
		super(module, resultReceiver);
	}

	@Override
	protected void handleTaskRefresh(ATaskMark taskMark, OperateResult result) {
		List<EventBannerInfo> discountBannerInfoCache = module.getCacheManager().getDiscountBannerInfoCache();
		discountBannerInfoCache.clear();
		List<DiscountCategoryInfo> discountCategoryInfoCache = module.getCacheManager().getDiscountCategoryInfoCache();
		discountCategoryInfoCache.clear();
		module.getCacheManager().getCityDiscountInfoCache().deleteItemInfoIndexFromCache(taskMark);
	}

	@Override
	public String TAG() {
		return FetchCityDiscountsTracker.class.getSimpleName();
	}

	@Override
	public void handleResult(OperateResult result) {
		PageableResult pageableResult = (PageableResult) result.getResultData();
		if (pageableResult.getContent() != null) {
			FetchCityDiscountsTaskMark aTaskMark = (FetchCityDiscountsTaskMark) result.getTaskMark();
			Map<String, Object> map = (Map<String, Object>) pageableResult.getContent();
			List<DiscountCategoryInfo> discountCategoryInfoCache = (List<DiscountCategoryInfo>) map
					.get(FetchCityDiscountsHandler.KEY_CATEGORIES);
			module.getCacheManager().setDiscountCategoryInfoCache(discountCategoryInfoCache);
			List<EventBannerInfo> discountBannerInfoCache = (List<EventBannerInfo>) map
					.get(FetchCityDiscountsHandler.KEY_HEAD_POSTS);
			module.getCacheManager().setDiscountBannerInfoCache(discountBannerInfoCache);
			List<DiscountInfo> discountInfoCache = (List<DiscountInfo>) map.get(FetchCityDiscountsHandler.KEY_POSTS);
			List<DiscountInfo> discounts = module.getCacheManager().getCityDiscountInfoCache()
					.getItemInfoList(aTaskMark);
			discounts.addAll(discountInfoCache);
			Collections.sort(discounts, new BUtils.DiscountInfosSorter());
			module.getCacheManager().getCityDiscountInfoCache().setItemInfoToCache(aTaskMark, discounts);
			List<HomePageInfo> businessHomeDeals = (List<HomePageInfo>) map.get(FetchCityDiscountsHandler.BUSINESS_HOME_DEALS);
			module.getCacheManager().setBusinessHomeDeals(businessHomeDeals);
			aTaskMark.setPageInfo(pageableResult.getPageInfo());
		}
	}
}
